
//获取应用实例
const app = getApp()
var commonData = require('../../data/data.js');
Page({
  data: {
    powerList: [],
    currentFight: [],
    currentPowerItem:{},
    currentSelectedIdList: [],
    isPowerShow:false,
    height:0,
    isChange: false//判断是否发生了勾选变化，
  },
  getInstanceById: function (id, list) {
    var res = null;
    for (var i = 0; i < list.length; i++) {
      if (id == list[i].id) {
        res = list[i];
        break;
      }
    }
    return res;
  },
  showDetail:function(e){
    var id = e.currentTarget.dataset.id;
    var instance = this.getInstanceById(id, this.data.powerList);
    this.setData({
      currentPowerItem: instance,
      isPowerShow: true
    })

  },
  hideDetail: function (e) {
      this.setData({
        currentPowerItem: {},
        isPowerShow: false
      })
  },

  onLoad: function () {
    var that = this;
    this.initData();
    this.updateData();
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    this.setData({
      isChange: true,
      currentSelectedIdList: e.detail.value
    });
  },
  saveSet: function () {
    var that = this;
    //如果没发生变化，就不需要提交
    if (!this.data.isChange) {
      app.showMsg("设置完成"); return;
    }else{
      //数组个数必须等于6
      var _currentSelectedIdList = this.data.currentSelectedIdList;
      if(_currentSelectedIdList.length!=6){
        app.showMsg("选择数量必须为6"); return;
      }
      wx.showLoading({
        title: '操作中 ',
      })
      wx.request({
        url: app.appSetting.host + "Weixin/SetFight",
        data: {
          UserId: app.globalData.userInfo.UserId,
          PowerIdList:_currentSelectedIdList
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res);
          wx.hideLoading();
          if (res.data.Status == 0) {
            app.showMsg("设置完成");
            //更新用户当前信息
            app.getUserInfo(app.globalData.userInfo.UserId);
            //还原逻辑数据
            that.setData({
              isChange: false,
              currentSelectedIdList: []
            });
          } else {
            app.showMsg(res.data.Message);
          }

        }
      })
    }
  },

  updateData:function(){
      var that = this;
      setInterval(function(){
        that.setData({
          currentFight: commonData.currentFightPowers
        })
        
      },1500)
  },

  initData: function () {
    var _idlist = [];
    for (var i = 0; i < commonData.powers.length; i++) {
      if (commonData.powers[i].isFight == 1) {
        _idlist.push(commonData.powers[i].id);
      }
    }
    this.setData({
      powerList: commonData.powers,
      currentFight: commonData.currentFightPowers,
      currentSelectedIdList: _idlist
    });
    this.getSystermMsg();
  },
  //获取屏幕高度，宽度
  getSystermMsg: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        commonData.screenInfo.height = res.windowHeight;
        that.setData({
          height: res.windowHeight
        })
      }
    })
  },
  onShow: function () {
    this.initData();
  },

})
